package com.unionServer;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;

/**
 * @description:
 * @author: wangxinal
 * @time: 2022/1/13 14:11
 */
public class MessageTest {

    @Test
    public void test01() throws MalformedURLException, DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/main/resources/CUP_service_parse_default.xml"));

        Element rootElm = document.getRootElement();
        listNodes(rootElm);

        System.out.println("123");
    }
    public static void listNodes(Element node) {
        System.out.println("当前节点的名称：：" + node.getName());
        // 获取当前节点的所有属性节点
        List<Attribute> list = node.attributes();
        // 遍历属性节点
        for (Attribute attr : list) {
            System.out.print( attr.getName()+ "-" +attr.getText() + "--"  + attr.getValue());
        }

        if (!(node.getTextTrim().equals(""))) {
            System.out.print("文本内容：：：：" + node.getText());
        }

        // 当前节点下面子节点迭代器
        Iterator<Element> it = node.elementIterator();
        // 遍历
        while (it.hasNext()) {
            // 获取某个子节点对象
            Element e = it.next();
            // 对子节点进行遍历
            listNodes(e);
        }
    }
}
